﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Entitys;
using Modules.Notify.Repositories;
using Modules.System.Attachment;
using PmSoft;
using static Modules.Notify.NotifyConstants;

namespace Modules.Notify
{
    [UseDI(ServiceLifetime.Scoped)]
    public class NotifyAttachmentService
    {
        private readonly NotifyAttachmentRepository repository;
        private readonly AttachmentService attachmentService;

        public NotifyAttachmentService(NotifyAttachmentRepository repository, AttachmentService attachmentService)
        {
            this.repository = repository;
            this.attachmentService = attachmentService;
        }

        public void CreateNotifyAttachment(NotifyAttachment entity)
        {
            if (string.IsNullOrEmpty(entity.AttachmentId))
                throw new ArgumentNullException(nameof(entity.AttachmentId));
            repository.Insert(entity);
            ToggleTemporaryAttachment(new List<NotifyAttachment> { entity });
        }

        public void DeletNotifyAttachments(IEnumerable<int> ids)
        {
            foreach (int id in ids)
                DeleteNotifyAttachment(id);
        }

        public void DeleteNotifyAttachment(int id)
        {
            repository.DeleteByEntityId(id);
        }

        public NotifyAttachment? GetNotifyAttachment(int id)
        {
            return repository.Get(id);
        }

        public void UpdateNotifyAttachment(NotifyAttachment entity)
        {
            repository.Update(entity);
        }

        /// <summary>
        /// 通过notifyId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetWorkerAttachmentByNotifyId(string notifyId)
        {
            var data = repository.GetAttachmentsByNotifyId(notifyId);
            return attachmentService.GetAttachmentInfos(data.Where(a => a.AttachmentType == 通知类型.工人通知).Select(a => a.AttachmentId));
        }

        /// <summary>
        /// 通过notifyId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetSysAttachmentByNotifyId(string notifyId)
        {
            var data = repository.GetAttachmentsByNotifyId(notifyId);
            return attachmentService.GetAttachmentInfos(data.Where(a => a.AttachmentType == 通知类型.系统通知).Select(a => a.AttachmentId));
        }

        /// <summary>
        /// 根据notifyId获取系统附件关联对象
        /// </summary>
        /// <param name="notifyId"></param>
        /// <param name="attachmentType">附件类型 1：系统通知 2：工人通知</param>
        /// <returns></returns>
        public IEnumerable<NotifyAttachment> GetNotifyAttachmentsByNotifyId(string notifyId, 通知类型 attachmentType)
        {
            return repository.GetAttachmentsByNotifyId(notifyId).Where(a => a.AttachmentType == attachmentType);
        }

        /// <summary>
        /// 上传临时附件到MinIO
        /// </summary>
        /// <param name="notifyAttachments"></param>
        public void ToggleTemporaryAttachment(IEnumerable<NotifyAttachment> notifyAttachments)
        {
            attachmentService.ToggleTemporaryAttachments(notifyAttachments);
        }
    }
}